<template>
  <a-modal
    :open="open"
    width="640px"
    :title="formState?.bqbh ? '修改房间' : '新增房间'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    :keyboard="false"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" :colon="false" class="form-style-flex">
      <a-form-item label="房间名称" class="w-1/2" v-bind="validateInfos.roomName">
        <a-input v-model:value="formState.roomName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="顺序号" class="w-1/2" v-bind="validateInfos.outOrder">
        <a-input v-model:value="formState.outOrder" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" placeholder="请输入" :rows="5" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  },
  wardId: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update:open', 'success'])
const formState = ref({
  roomName: '',
  outOrder: ''
})
const rulesState = ref({
  roomName: [{ required: true, message: '请输入房间名称' }],
  outOrder: [{ required: true, message: '请输入顺序号' }]
})
const { isCompleted, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  const { code, msg } = await post('/operation/opex/doSaveDictWardRoom', { ...formState.value, wardId: props.wardId })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('success')
}
const onCancel = () => {
  emit('update:open', false)
}
watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = { ...props.rowData }
    }
  }
)
</script>
<style lang="less" scoped></style>
